home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 419_01 / odmg10 / lib / odmg / Collection.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-28  |  10.3 KB  |  488 lines

  1. /******************************* <+>***************************
  2.  **                             DTA
  3.  *************************************************************
  4.  **
  5.  **  $Id: Collection.h,v 1.6 1994/03/01 23:01:20 dta Exp $
  6.  **
  7.  **  $Source: /cvs/lib/odmg/Collection.h,v $
  8.  **
  9.  **  What @(#):
  10.  **
  11.  **  Author: Dale T. Anderson
  12.  **
  13.  ******************************* <+>***************************/
  14.  
  15. #ifndef Collection_h
  16. #define Collection_h
  17.  
  18. #include <Odmg/Ref.h>
  19. #include <Odmg/Iterator.h>
  20.  
  21. template <class T> class Collection;
  22.  
  23. template <class T>
  24. class Collection
  25. {
  26.     protected:
  27.  
  28.     Position m_count;
  29.     boolean m_is_ordered;
  30.     boolean m_allows_duplicates;
  31.  
  32.     public:
  33.  
  34.     Position cardinality (void) const;
  35.     boolean empty (void) const;
  36.  
  37.     boolean ordered (void) const;
  38.     boolean allows_duplicates (void) const;
  39.  
  40.     protected:
  41.  
  42.     Collection (void);
  43.  
  44.     public:
  45.  
  46.     virtual ~Collection (void);
  47.  
  48.     boolean operator == (Ref <Collection <T> >) const;
  49.     boolean operator != (Ref <Collection <T> >) const;
  50.  
  51.     Collection <T> &operator = (Ref <Collection <T> >);
  52.  
  53.     Ref <Collection <T> > copy (void) const;
  54.  
  55.     Ref <Iterator <T> > create_iterator (boolean stable = FALSE) const;
  56.  
  57.     virtual void insert_element (Ref <T>);
  58.     void operator += (Ref <T>);
  59.  
  60.     virtual void remove_element (Ref <T>);
  61.     void operator -= (Ref <T>);
  62.  
  63.     virtual void remove_element_at (Iterator <T> &);
  64.     virtual void replace_element_at (Ref <T>, const Iterator <T> &);
  65.     virtual Ref <T> retrieve_element_at (const Iterator <T> &);
  66.  
  67.     virtual Ref <T> select_element (const char *);
  68.     virtual Iterator <T> select (Iterator <T> , const char *);
  69.     virtual int query (Ref <Collection <T> > &, const char *);
  70.  
  71.     virtual boolean exists_element (const char *) const;
  72.     virtual boolean contains_element (Ref <T>) const;
  73.  
  74.     // Additions
  75.  
  76.     Ref <Collection <T> > _union (const Ref <Collection <T> >) const;
  77.     Ref <Collection <T> > intersection (const Ref <Collection <T> >) const;
  78.     Ref <Collection <T> > difference (const Ref <Collection <T> >) const;
  79.  
  80.     Ref <Collection <T> > operator + (const Ref <Collection <T> >) const;
  81.     Ref <Collection <T> > operator * (const Ref <Collection <T> >) const;
  82.     Ref <Collection <T> > operator - (const Ref <Collection <T> >) const;
  83.  
  84.     Ref <Collection <T> > operator += (const Ref <Collection <T> >);
  85.     Ref <Collection <T> > operator *= (const Ref <Collection <T> >);
  86.     Ref <Collection <T> > operator -= (const Ref <Collection <T> >);
  87.  
  88.     boolean is_subset_of (const Ref <Collection <T> >) const;
  89.     boolean is_proper_subset_of (const Ref <Collection <T> >) const;
  90.     boolean is_superset_of (const Ref <Collection <T> >) const;
  91.     boolean is_proper_superset_of (const Ref <Collection <T> >) const;
  92.  
  93.     virtual Ref <T> retrieve_element_at (const Position);
  94.  
  95.     void remove_all (void);
  96.     void delete_all (void);
  97.  
  98.     virtual Ref <Collection <T> > create (void) const;
  99. };
  100.  
  101.  
  102. //
  103. // Methods
  104. //
  105.  
  106. template <class T>
  107. Position Collection <T>::cardinality (void) const
  108. {
  109.     return m_count;
  110. }
  111.  
  112. template <class T>
  113. boolean Collection <T>::empty (void) const
  114. {
  115.     return m_count == 0;
  116. }
  117.  
  118. template <class T>
  119. boolean Collection <T>::ordered (void) const
  120. {
  121.     return m_is_ordered;
  122. }
  123.  
  124. template <class T>
  125. boolean Collection <T>::allows_duplicates (void) const
  126. {
  127.     return m_allows_duplicates;
  128. }
  129.  
  130. template <class T>
  131. Collection <T>::Collection (void) :
  132.     m_is_ordered (TRUE),
  133.     m_allows_duplicates (TRUE),
  134.     m_count (0)
  135. {
  136. }
  137.  
  138. template <class T>
  139. Collection <T>::~Collection (void)
  140. {
  141. }
  142.  
  143. template <class T>
  144. boolean Collection <T>::operator == (Ref <Collection <T> > c) const
  145. {
  146.     if (cardinality () != c->cardinality ())
  147.     return FALSE;
  148.  
  149.     Iterator <T> iter1 (c);
  150.     Iterator <T> iter2 (this);
  151.  
  152.     Ref <T> ref1;
  153.     Ref <T> ref2;
  154.  
  155.     boolean isequal = TRUE;
  156.  
  157.     while (isequal && iter1.next (ref1) && iter2.next (ref2))
  158.     if (ref1 != ref2)
  159.         isequal = FALSE;
  160.  
  161.     return isequal;
  162. }
  163.  
  164. template <class T>
  165. boolean Collection <T>::operator != (Ref <Collection <T> > c) const
  166. {
  167.     return !(c == (Ref <Collection <T> > &) *this);
  168. }
  169.  
  170. template <class T>
  171. Collection <T> &Collection <T>::operator = (Ref <Collection <T> > c)
  172. {
  173.     remove_all ();
  174.  
  175.     Iterator <T> iter (c);
  176.     Ref <T> ref; 
  177.  
  178.     while (iter.next (ref))
  179.     insert_element (ref);
  180.  
  181.     return *this;
  182. }
  183.  
  184. template <class T>
  185. Ref <Collection <T> > Collection <T>::copy (void) const
  186. {
  187.     Ref <Collection <T> > c (create ());
  188.  
  189.     Iterator <T> iter ((Collection <T> *) this);
  190.     Ref <T> ref;
  191.  
  192.     while (iter.next (ref))
  193.     c->insert_element (ref);
  194.     
  195.     return c;
  196. }
  197.  
  198. template <class T>
  199. Ref <Iterator <T> > Collection <T>::create_iterator (boolean) const
  200. {
  201.     return Ref <Iterator <T> > (new Iterator <T> ((Collection <T> *) this));
  202. }
  203.  
  204. template <class T>
  205. void Collection <T>::insert_element (Ref <T>)
  206. {
  207.     cerr << "insert_element - not implemented." << endl;
  208. }
  209.  
  210. template <class T>
  211. void Collection <T>::operator += (Ref <T> ref)
  212. {
  213.     insert_element (ref);
  214. }
  215.  
  216. template <class T>
  217. void Collection <T>::remove_element (Ref <T>)
  218. {
  219.     cerr << "remove_element - not implemented." << endl;
  220. }
  221.  
  222. template <class T>
  223. void Collection <T>::operator -= (Ref <T> ref)
  224. {
  225.     remove_element (ref);
  226. }
  227.  
  228. template <class T>
  229. void Collection <T>::remove_element_at (Iterator <T> &iter)
  230. {
  231.     Position position = iter.get_position ();
  232.     if (position >= 0)
  233.     iter.set_position (position - 1);
  234.     remove_element (retrieve_element_at (position));
  235. }
  236.  
  237. template <class T>
  238. void Collection<T>::replace_element_at (Ref<T>, const Iterator <T> &)
  239. {
  240.     cerr << "replace_element_at - not implemented" << endl;
  241. }
  242.  
  243. template <class T>
  244. Ref <T> Collection <T>::retrieve_element_at (const Iterator <T> &iter)
  245. {
  246.     return retrieve_element_at (iter.get_position ());
  247. }
  248.  
  249. template <class T>
  250. Ref <T> Collection <T>::select_element (const char *)
  251. {
  252.     cerr << "select_element_at - not implemented." << endl;
  253.     return Ref <T> (NULL);
  254. }
  255.  
  256. template <class T>
  257. Iterator <T> Collection <T>::select (Iterator <T> iter, const char *)
  258. {
  259.     cerr << "select - not implemented." << endl;
  260.     return iter;
  261. }
  262.  
  263. template <class T>
  264. int Collection <T>::query (Ref <Collection <T> >&, const char *)
  265. {
  266.     cerr << "query - not implemented." << endl;
  267.     return -1;
  268. }
  269.  
  270. template <class T>
  271. boolean Collection <T>::exists_element (const char *) const
  272. {
  273.     cerr << "exists_element - not implemented." << endl;
  274.     return FALSE;
  275. }
  276.  
  277. template <class T>
  278. boolean Collection <T>::contains_element (Ref <T> target) const
  279. {
  280.     Iterator <T> iter (this);
  281.     Ref <T> ref; 
  282.  
  283.     while (iter.next (ref))
  284.     if (ref == target)
  285.         return TRUE;
  286.  
  287.     return FALSE;
  288. }
  289.  
  290. //
  291. // Additions
  292. //
  293.  
  294. template <class T>
  295. Ref <Collection <T> > Collection<T>::_union (const Ref <Collection <T> > c1) const
  296. {
  297.     Ref <Collection <T> > c2 (copy ());
  298.     Iterator <T> iter (c1);
  299.     Ref <T> ref;
  300.  
  301.     while (iter.next (ref))
  302.     if (c2->allows_duplicates () || !c2->contains_element (ref))
  303.         c2->insert_element (ref);
  304.  
  305.     return c2;
  306. }
  307.  
  308. template <class T>
  309. Ref <Collection <T> > Collection<T>::intersection (const Ref <Collection <T> > c1) const
  310. {
  311.     Ref <Collection <T> > c2 (create ());
  312.     Iterator <T> iter (this);
  313.     Ref <T> ref;
  314.  
  315.     while (iter.next (ref))
  316.     if (c1->contains_element (ref))
  317.         c2->insert_element (ref);
  318.  
  319.     return c2;
  320. }
  321.  
  322. template <class T>
  323. Ref <Collection <T> > Collection<T>::difference (const Ref <Collection <T> > c1) const
  324. {
  325.     Ref <Collection <T> > c2 (create ());
  326.     Iterator <T> iter (this);
  327.     Ref <T> ref;
  328.  
  329.     while (iter.next (ref))
  330.     if (!c1->contains_element (ref))
  331.         c2->insert_element (ref);
  332.  
  333.     return c2;
  334. }
  335.  
  336. template <class T>
  337. Ref <Collection <T> > Collection<T>::operator += (const Ref <Collection <T> > c1)
  338. {
  339.     Iterator <T> iter (c1);
  340.     Ref <T> ref;
  341.  
  342.     while (iter.next (ref))
  343.     if (allows_duplicates () || !contains_element (ref))
  344.         insert_element (ref);
  345.  
  346.     return Ref <Collection <T> > (this);
  347. }
  348.  
  349. template <class T>
  350. Ref <Collection <T> > Collection<T>::operator *= (const Ref <Collection <T> > c1)
  351. {
  352.     Iterator <T> iter (this);
  353.     Ref <T> ref;
  354.  
  355.     while (iter.next (ref))
  356.     if (!c1->contains_element (ref))
  357.         remove_element_at (iter);
  358.  
  359.     return Ref <Collection <T> > (this);
  360. }
  361.  
  362. template <class T>
  363. Ref <Collection <T> > Collection<T>::operator -= (const Ref <Collection <T> > c1)
  364. {
  365.     Iterator <T> iter (c1);
  366.     Ref <T> ref;
  367.  
  368.     while (iter.next (ref))
  369.     while (contains_element (ref))
  370.         remove_element (ref);
  371.  
  372.     return Ref <Collection <T> > (this);
  373. }
  374.  
  375. template <class T>
  376. Ref <Collection <T> > Collection<T>::operator + (const Ref <Collection <T> > c) const
  377. {
  378.     return _union (c);
  379. }
  380.  
  381. template <class T>
  382. Ref <Collection <T> > Collection<T>::operator * (const Ref <Collection <T> > c) const
  383. {
  384.     return intersection (c);
  385. }
  386.  
  387. template <class T>
  388. Ref <Collection <T> > Collection<T>::operator - (const Ref <Collection <T> > c) const
  389. {
  390.     return difference (c);
  391. }
  392.  
  393. template <class T>
  394. boolean Collection <T>::is_subset_of (const Ref <Collection <T> > c) const
  395. {
  396.     Iterator <T> iter (this);
  397.     Ref <T> ref;
  398.  
  399.     while (iter.next (ref))
  400.     if (!c->contains_element (ref))
  401.         return FALSE;
  402.  
  403.     return TRUE;
  404. }
  405.  
  406. template <class T>
  407. boolean Collection <T>::is_proper_subset_of (const Ref <Collection <T> > c) const
  408. {
  409.     Iterator <T> iter (this);
  410.     Ref <T> ref;
  411.  
  412.     if (cardinality () != c->cardinality ())
  413.     return FALSE;
  414.  
  415.     while (iter.next (ref))
  416.     if (!c->contains_element (ref))
  417.         return FALSE;
  418.  
  419.     return TRUE;
  420. }
  421.  
  422. template <class T>
  423. boolean Collection <T>::is_superset_of (const Ref <Collection <T> > c) const
  424. {
  425.     Iterator <T> iter (c);
  426.     Ref <T> ref;
  427.  
  428.     while (iter.next (ref))
  429.     if (!contains_element (ref))
  430.         return FALSE;
  431.  
  432.     return TRUE;
  433. }
  434.  
  435. template <class T>
  436. boolean Collection <T>::is_proper_superset_of (const Ref <Collection <T> > c) const
  437. {
  438.     Iterator <T> iter (c);
  439.     Ref <T> ref;
  440.  
  441.     if (cardinality () != c->cardinality ())
  442.     return FALSE;
  443.  
  444.     while (iter.next (ref))
  445.     if (!contains_element (ref))
  446.         return FALSE;
  447.  
  448.     return TRUE;
  449. }
  450.  
  451. template <class T>
  452. Ref <T> Collection <T>::retrieve_element_at (const Position)
  453. {
  454.     cerr << "retrieve_element_at - not implemented" << endl;
  455.     return Ref <T> (NULL);
  456. }
  457.  
  458. template <class T>
  459. void Collection<T>::remove_all (void)
  460. {
  461.     Iterator <T> iter (this);
  462.     Ref <T> ref; 
  463.  
  464.     while (iter.next (ref))
  465.     remove_element_at (iter);
  466. }
  467.  
  468. template <class T>
  469. void Collection<T>::delete_all (void)
  470. {
  471.     Iterator <T> iter (this);
  472.     Ref <T> ref; 
  473.  
  474.     while (iter.next (ref))
  475.     delete (T *) ref;
  476.  
  477.     remove_all ();
  478. }
  479.  
  480. template <class T>
  481. Ref <Collection <T> > Collection <T>::create (void) const
  482. {
  483.     cerr << "create - not implemented" << endl;
  484.     return Ref <Collection <T> > (NULL);
  485. }
  486.  
  487. #endif
  488.